//
//  ViewController.m
//  BaiduMapBase
//
//  Created by wjj on 14/11/18.
//  Copyright (c) 2014年 wjj. All rights reserved.
//

#import "ViewController.h"
#import "BMapKit.h"

@interface ViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKRouteSearchDelegate>
{
    BMKMapView *mapView;
    BMKLocationService *locationService;
    BMKPointAnnotation *_annotation;
    BMKRouteSearch *_rouchSearch;
    CLLocationCoordinate2D _userLocation;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self startLocation];
    [self createMapView];
    [self addRouteSearch];
//    [self addHeatMap];
    
}

//创建地图
- (void)createMapView
{
    mapView = [[BMKMapView alloc] initWithFrame:self.view.bounds];
    
    //地图类型
    //1.卫星地图
    //[mapView setMapType:BMKMapTypeSatellite];
    //2.普通地图
    //[mapView setMapType:BMKMapTypeStandard];
    //3.实时路况地图
    [mapView setMapType:BMKMapTypeTrafficOn];
    
    [self.view addSubview:mapView];
}

- (void)startLocation
{
    locationService = [[BMKLocationService alloc] init];
    locationService.delegate = self;
    [locationService startUserLocationService];
    mapView.showsUserLocation = YES;//显示定位图层
}

//增加标注
- (void)addAnnotation
{
    _annotation = [[BMKPointAnnotation alloc] init];
    //创建经纬度坐标
//    CLLocationCoordinate2D coor;
//    coor.latitude = 39.915;
//    coor.longitude = 116.404;
//    annotation.coordinate = _userLocation;
    _annotation.title = @"您所在的位置";
    
    [mapView addAnnotation:_annotation];
}

- (void)addHeatMap
{
    //创建热力图数据类
    BMKHeatMap* heatMap = [[BMKHeatMap alloc]init];
    //创建渐变色类
    UIColor *color1 = [UIColor blueColor];
    UIColor *color2 = [UIColor yellowColor];
    UIColor *color3 = [UIColor redColor];
    NSArray *colorInitialArray = [[NSArray alloc] initWithObjects:color1,color2,color3, nil];
    BMKGradient *gradient = [[BMKGradient alloc] initWithColors:colorInitialArray startPoints:@[@"0.08f", @"0.4f", @"1f"]];
    
    //如果用户自定义了渐变色则按自定义的渐变色进行绘制否则按默认渐变色进行绘制
    heatMap.mGradient = gradient;
    
    //创建热力图数据数组
    NSMutableArray *data = [NSMutableArray array];
    int num = 1000;
    for(int i = 0; i<num; i++)
    {
        //创建BMKHeatMapNode
        BMKHeatMapNode *heapmapnode_test = [[BMKHeatMapNode alloc]init];
        //此处示例为随机生成的坐标点序列，开发者使用自有数据即可
        CLLocationCoordinate2D coor;
        float random = (arc4random()%1000)*0.001;
        float random2 = (arc4random()%1000)*0.003;
        float random3 = (arc4random()%1000)*0.015;
        float random4 = (arc4random()%1000)*0.016;
        if(i%2 == 0)
        {
            coor.latitude = 39.915+random;
            coor.longitude = 116.403+random2;
        }
        else
        {
            coor.latitude = 39.915-random3;
            coor.longitude = 116.403-random4;
        }
        heapmapnode_test.pt = coor;
        //随机生成点强度
        heapmapnode_test.intensity = arc4random()*900;
        //添加BMKHeatMapNode到数组
        [data addObject:heapmapnode_test];
    }
    //将点数据赋值到热力图数据类
    heatMap.mData = data;
    //调用mapView中的方法根据热力图数据添加热力图
    [mapView addHeatMap:heatMap];
    
}

- (void)addRouteSearch
{
    _rouchSearch = [[BMKRouteSearch alloc]init];
    _rouchSearch.delegate = self;
    //发起检索
    BMKPlanNode *start = [[BMKPlanNode alloc] init];
    start.name = @"华南师范大学石牌校区";
    BMKPlanNode *end = [[BMKPlanNode alloc] init];
    end.name = @"北京路";
    BMKTransitRoutePlanOption *transitRouteSearchOption = [[BMKTransitRoutePlanOption alloc]init];
    transitRouteSearchOption.city= @"广州市";
    transitRouteSearchOption.from = start;
    transitRouteSearchOption.to = end;
    BOOL flag = [_rouchSearch transitSearch:transitRouteSearchOption];
    if(flag)
    {
        NSLog(@"bus检索发送成功");
    }
    else
    {
        NSLog(@"bus检索发送失败");
    }
}

//删除热力图
-(void)removeHeatMap
{
    [mapView removeHeatMap];
}


- (void)viewDidAppear:(BOOL)animated
{
    [self addAnnotation];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    mapView.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    mapView.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]])
    {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorGreen;
        newAnnotationView.animatesDrop = YES;// 设置该标注点显示时是否有动画
        return newAnnotationView;
    }
    return nil;
}

#pragma BMKLocationServiceDelegate
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
//    NSLog(@"heading is %@",userLocation.heading);
}

//处理位置坐标更新
- (void)didUpdateUserLocation:(BMKUserLocation *)userLocation
{
//    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
//    _userLocation = userLocation.location.coordinate;
    _annotation.coordinate = userLocation.location.coordinate;
    mapView.centerCoordinate = userLocation.location.coordinate;
    [mapView updateLocationData:userLocation];
}

#pragma BMKRouteSearchDelegate
- (void)onGetTransitRouteResult:(BMKRouteSearch*)searcher result:(BMKTransitRouteResult*)result
                     errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR)
    {
        for(BMKTransitRouteLine *item in result.routes)
        {
//            NSLog(item.)
        }
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
        //当路线起终点有歧义时通，获取建议检索起终点
        //r··   esult.routeAddrResult
    }
    else {
        NSLog(@"抱歉，未找到结果");
    }
}

@end
